using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    class HWScriptSignalOut : HWScriptCommand 
    {    
        SignalSource oSignalSource;
        string sValue;
        SignalFormat eSignalFormat;

        public HWScriptSignalOut(RapidHardware poRapidHardware, int piClockTime, string psSignalName, string psValue, SignalFormat peSignalFormat)
            : base(poRapidHardware,piClockTime, -1)
        {
            oSignalSource = oRapidHardware.SystemInterface.GetSignalSource(psSignalName);
            if (oSignalSource == null)
                throw new Exception("Signal source named " + psSignalName + " does not exist.");
            sValue = psValue;
            eSignalFormat = peSignalFormat;
        }

        public HWScriptSignalOut(RapidHardware poRapidHardware, int piClockTime, string psSignalName, string psValue)
            : base(poRapidHardware, piClockTime, -1)
        {
            oSignalSource = oRapidHardware.SystemInterface.GetSignalSource(psSignalName);
            if (oSignalSource == null)
                throw new Exception("Signal source named " + psSignalName + " does not exist.");
            sValue = psValue;
            eSignalFormat = oSignalSource.SignalFormat;
        }        

        public override string DoCommand()
        {
            oSignalSource.SignalFormat = eSignalFormat;
            oSignalSource.FormattedValue = sValue;
            return "set " + oSignalSource.Name + " = {" + oSignalSource.FormattedValue + "}"; 
        }

        public override string ToVerilog()
        {
            string sBinaryValue = Conversion.StringFormatToBinary(sValue, eSignalFormat, oSignalSource.Width);
            return oSignalSource.NameHDL + " = {" + oSignalSource.Width.ToString() + "'b" + sBinaryValue + "}";
        }

        public override string SignalNameHDL()
        {
            return oSignalSource.NameHDL;
        }

        public override string SignalValue()
        {
            string sBinaryValue = Conversion.StringFormatToBinary(sValue, eSignalFormat, oSignalSource.Width);
            return sBinaryValue;            
        }
    }
}
